#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+2;
const int mod=1e9+7;
int n;
int fa[maxn],son[maxn][2],ty[maxn],pson[maxn][2],pty[maxn],pfa[maxn],dep[maxn];
int sum[maxn];
int rt,pasrt;
void rotate(int x){
    int t=ty[x],f=fa[x],ff=fa[fa[x]];
    son[f][t]=son[x][t^1],fa[son[x][t^1]]=f,ty[son[x][t^1]]=t;
    fa[f]=x,son[x][t^1]=f,fa[x]=ff,son[ff][ty[f]]=x;
    ty[x]=ty[f],ty[f]=t^1;
    if(!ff)rt=x;
    return ;
}
void splay(int x){
    // printf("try\n");
    if(x==rt)return ;
    while(fa[x]!=rt&&fa[x])rotate(fa[x]),rotate(x);
    if(x!=rt)rotate(x);
    // printf("end\n");
    return ;
}
void refix(){
    rt=pasrt;
    for(int i=1;i<=n;i++)fa[i]=pfa[i],ty[i]=pty[i],son[i][0]=pson[i][0],son[i][1]=pson[i][1];
    return ;
}
void dfs(int x){
    if(!x)return ;
    dep[x]=dep[fa[x]]+1;
    // printf("dep[%d]=%d\n",x,dep[x]);
    sum[x]=(sum[x]+dep[x])%mod;
    dfs(son[x][0]);
    dfs(son[x][1]);
    return ;
}
int read(){
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch))f=(ch=='-')?(-f):f,ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return x*f;
}
int ot[40],tp;
void write(int x){
    if(!x){putchar('0');return ;}
    while(x){ot[++tp]=x%10,x/=10;}
    while(tp)putchar('0'+ot[tp--]);
    return ;
}
int main(){
    freopen("dream.in","r",stdin);
    freopen("dream.out","w",stdout);
    // scanf("%d",&n);
    n=read();
    for(int i=1;i<=n;i++){
        // scanf("%d%d",&son[i][0],&son[i][1]);
        son[i][0]=read(),son[i][1]=read();
        if(son[i][0])fa[son[i][0]]=i,ty[son[i][0]]=0;
        if(son[i][1])fa[son[i][1]]=i,ty[son[i][1]]=1;
    }
    pasrt=rt=1;
    for(int i=1;i<=n;i++)pfa[i]=fa[i],pty[i]=ty[i],pson[i][0]=son[i][0],pson[i][1]=son[i][1];
    dep[0]=-1;
    for(int i=1;i<=n;i++){
        // printf("sp %d\n",i);
        refix();
        splay(i);
        dfs(rt);
    }
    for(int i=1;i<=n;i++)write(sum[i]),putchar('\n');
    return 0;
}